package com.example.hotHub.controller;

import com.example.hotHub.common.R;
import com.example.hotHub.model.ListData;
import com.example.hotHub.model.NormalHotSearch;
import com.example.hotHub.service.HotSearchWrapperService;
import com.example.hotHub.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@CrossOrigin
@Api(tags = "热搜爬取控制器")
@RequestMapping("hot")
@RestController
public class HotSearchWrapperController {

    @Autowired
    private HotSearchWrapperService hotSearchWrapperService;

    @Autowired
    private RedisUtil redisUtil;

    @GetMapping("baidu")
    @ApiOperation("百度热搜")
    public R getBaiduHotSearchList() {
        List<NormalHotSearch> list;
        synchronized (hotSearchWrapperService) {
            list = (List<NormalHotSearch>) redisUtil.get("baidu");
        }
        if (list == null){
            list = hotSearchWrapperService.getBaiduHotSearchList();
            redisUtil.set("baidu", list);
        }
        return R.success(new ListData(list));
    }

    @GetMapping("weibo")
    @ApiOperation("微博热搜")
    public R getWeiboHotSearchList() {
        List<NormalHotSearch> list;
        synchronized (hotSearchWrapperService) {
            list = (List<NormalHotSearch>) redisUtil.get("weibo");
        }
        if (list == null){
            list = hotSearchWrapperService.getWeiboHotSearchList();
            redisUtil.set("weibo", list);
        }
        return R.success(new ListData(list));
    }

    @GetMapping("toutiao")
    @ApiOperation("头条热搜")
    public R getToutiaoHotSearchList() {
        List<NormalHotSearch> list;
        synchronized (hotSearchWrapperService) {
            list = (List<NormalHotSearch>) redisUtil.get("toutiao");
        }
        if (list == null){
            list = hotSearchWrapperService.getToutiaoHotSearchList();
            redisUtil.set("toutiao", list);
        }
        return R.success(new ListData(list));
    }

    @GetMapping("zhihu")
    @ApiOperation("知乎热搜")
    public R getZhihuHotSearchList() {
        List<NormalHotSearch> list;
        synchronized (hotSearchWrapperService) {
            list = (List<NormalHotSearch>) redisUtil.get("zhihu");
        }
        if (list == null){
            list = hotSearchWrapperService.getZhihuHotSearchList();
            redisUtil.set("zhihu", list);
        }
        return R.success(new ListData(list));
    }

    @GetMapping("tencent")
    @ApiOperation("腾讯热搜")
    public R getTencentHotSearchList() {
        List<NormalHotSearch> list;
        synchronized (hotSearchWrapperService) {
            list = (List<NormalHotSearch>) redisUtil.get("tencent");
        }
        if (list == null){
            list = hotSearchWrapperService.getTencentHotSearchList();
            redisUtil.set("tencent", list);
        }
        return R.success(new ListData(list));
    }

    @GetMapping("bili")
    @ApiOperation("BiliBili热搜")
    public R getBiliHotSearchList(@RequestParam(defaultValue = "all") String type) {
        List<NormalHotSearch> list;
        synchronized (hotSearchWrapperService) {
            list = (List<NormalHotSearch>) redisUtil.get("bili");
        }
        if (list == null){
            list = hotSearchWrapperService.getBiliHotSearchList(type);
            redisUtil.set("bili", list);
        }
        return R.success(new ListData(list));
    }

    @GetMapping("haokan")
    @ApiOperation("好看视频热搜")
    public R getHaokanSearchList() {
        List<NormalHotSearch> list;
        synchronized (hotSearchWrapperService) {
            list = (List<NormalHotSearch>) redisUtil.get("haokan");
        }
        if (list == null){
            list = hotSearchWrapperService.getHaokanHotSearchList();
            redisUtil.set("haokan", list);
        }
        return R.success(new ListData(list));
    }

    @GetMapping("paper")
    @ApiOperation("澎湃热搜")
    public R getPaperHotSearchList(@RequestParam(defaultValue = "1") Integer day) {
        List<NormalHotSearch> normalHotSearchList = hotSearchWrapperService.getPaperHotSearchList(day);
        return R.success(new ListData<>(normalHotSearchList));
    }

    @GetMapping("52")
    @ApiOperation("吾爱破解热搜")
    public R get52PoJieHotSearchList(@RequestParam(defaultValue = "heats") String view, @RequestParam(defaultValue = "1") Integer day) {
        List<NormalHotSearch> normalHotSearchList = hotSearchWrapperService.get52PoJieHotSearchList(view, day);
        return R.success(new ListData<>(normalHotSearchList));
    }

    @GetMapping("url2Base64")
    public R getBase64(String url) throws UnsupportedEncodingException {
//        String base64 = null; (String) redisUtil.get(url);
//        if (StringUtils.isEmpty(base64)) {
//            base64 = getImage(url).toString();
//            redisUtil.set(url, base64);
//        }
        return R.success("data:image/jpg;base64," + new String(getImage(url), "ASCII"));
    }

    private byte[] getImage(String url) {
        org.springframework.web.client.RestTemplate restTemplate = new org.springframework.web.client.RestTemplate();
        /**
         * 请求地址
         * 请求方式
         * 请求实体类
         * 请求返回类型
         */
        ResponseEntity<byte[]> responseEntity = restTemplate.exchange(url, HttpMethod.GET, null, byte[].class);
        byte[] body = responseEntity.getBody();
        return body;
    }

}
